package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.SmsSetService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.sms.pojo.dto.SmsSignDelDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsSignInfoDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsSignSaveDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsTemplateDelDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsTemplateListDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsTemplateSaveDTO;
import com.xbongbong.pro.sms.pojo.vo.SmsSignDelVO;
import com.xbongbong.pro.sms.pojo.vo.SmsSignInfoVO;
import com.xbongbong.pro.sms.pojo.vo.SmsSignSaveVO;
import com.xbongbong.pro.sms.pojo.vo.SmsTemplateDelVO;
import com.xbongbong.pro.sms.pojo.vo.SmsTemplateListVO;
import com.xbongbong.pro.sms.pojo.vo.SmsTemplateSaveVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author: hongxiao
 * @Description: 短信设置
 * @Date: 2019/1/8 3:13 PM
 * @Modified By:
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/sms/set")
public class SmsSetController {
    private static final Logger LOG = LoggerFactory.getLogger(SmsSetController.class);

    @Resource
    private SmsSetService smsSetService;

    /**
     * 获取短信配置
     * @param br
     * @param smsSignInfoDTO 入参
     * @return
     *
     * @author hongxiao
     * @addTime: 2019-01-13 11:12
     * @since
     *
     * @modified by:
     * @updateTime: 2019-01-13 11:12
     * @version
     */
    @RequestMapping(value = "/signInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getConfig(@RequestBody @Valid SmsSignInfoDTO smsSignInfoDTO, BindingResult br) {
        XbbResponse<SmsSignInfoVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsSignInfoVO smsSignInfoVO = smsSetService.signInfo(smsSignInfoDTO);
                response = new XbbResponse<>(smsSignInfoVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/templateList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String templateList(@RequestBody @Valid SmsTemplateListDTO smsTemplateListDTO, BindingResult br) {
        XbbResponse<SmsTemplateListVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsTemplateListVO smsTemplateListVO = smsSetService.templateList(smsTemplateListDTO);
                response = new XbbResponse<>(smsTemplateListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }



    /**
     *  保存短信模板
     * @param br
     * @param smsTemplateSaveDTO 入参
     * @return
     *
     * @author hongxiao
     * @addTime: 2019-01-13 11:09
     * @since
     *
     * @modified by:
     * @updateTime: 2019-01-13 11:09
     * @version
     */
    @RequestMapping(value = "/templateSave", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String templateSave(@RequestBody @Valid SmsTemplateSaveDTO smsTemplateSaveDTO, BindingResult br) {
        XbbResponse<SmsTemplateSaveVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsTemplateSaveVO smsTemplateSaveVO  = smsSetService.templateSave(smsTemplateSaveDTO);
                response = new XbbResponse<>(smsTemplateSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 保存短信签名
     * @param br
     * @param smsSignSaveDTO 入参
     * @return
     * 
     * @author hongxiao
     * @addTime: 2019-01-13 11:11
     * @since
     * 
     * @modified by: 
     * @updateTime: 2019-01-13 11:11
     * @version
     */
    @RequestMapping(value = "/signSave", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String signSave(@RequestBody @Valid SmsSignSaveDTO smsSignSaveDTO, BindingResult br) {
        XbbResponse<SmsSignSaveVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsSignSaveVO smsSignSaveVO = smsSetService.signSave(smsSignSaveDTO);
                response = new XbbResponse<>(smsSignSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除失败的短信新签名
     * @param br
     * @param smsSignDelDTO 入参
     * @return
     * 
     * @author hongxiao
     * @addTime: 2019-01-13 11:11
     * @since
     * 
     * @modified by: 
     * @updateTime: 2019-01-13 11:11
     * @version
     */
    @RequestMapping(value = "/failSignDelete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String failSignDelete(@RequestBody @Valid SmsSignDelDTO smsSignDelDTO, BindingResult br) {
        XbbResponse<SmsSignDelVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsSignDelVO smsSignDelVO = smsSetService.signNewDel(smsSignDelDTO);
                response = new XbbResponse<>(smsSignDelVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除短信模板
     * @param br
     * @param smsTemplateDelDTO 入参
     * @return
     * 
     * @author hongxiao
     * @addTime: 2019-01-13 11:11
     * @since
     * 
     * @modified by: 
     * @updateTime: 2019-01-13 11:11
     * @version
     */
    @RequestMapping(value = "/templateDelete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String templateDelete(@RequestBody @Valid SmsTemplateDelDTO smsTemplateDelDTO, BindingResult br) {
        XbbResponse<SmsTemplateDelVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsTemplateDelVO smsTemplateDelVO = smsSetService.templateDel(smsTemplateDelDTO);
                response = new XbbResponse<>(smsTemplateDelVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
